﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using WorkoutTracker.ViewModelNameSpace;

namespace WorkoutTracker
{
    public partial class MainPage : PhoneApplicationPage
    {
        private ViewModel vm;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            vm = new ViewModel();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            // Later, you will replace this next line with something better.
            if (!StateUtilities.IsLaunching && this.State.ContainsKey("Workouts"))
            {
                // Old instance of the application
                // The user started the application from the Back button.

                vm = (ViewModel)this.State["Workouts"];
                //MessageBox.Show("Got data from state");
            }
            else
            {
                // New instance of the application
                // The user started the application from the application list,
                // or there is no saved state available.
                vm.GetWorkouts();
                //MessageBox.Show("Did not get data from state");
            }

            // There are two different views, but only one view model.
            // So, use LINQ queries to populate the views.

            // Set the data context for the Item view.
            this.DataContext = vm.Workouts;
            
            // If there is only one view, you could use the following code
            // to populate the view.
            //AccomplishmentViewOnPage.DataContext = vm.Accomplishments;

        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            if (this.State.ContainsKey("Workouts"))
            {
                this.State["Workouts"] = vm;
            }
            else
            {
                this.State.Add("Workouts", vm);
            }

            StateUtilities.IsLaunching = false;
        }


        private void AppBarSave_Click(object sender, EventArgs e)
        {
            vm.SaveWorkouts();
        }

        private void AppBarAdd_Click(object sender, EventArgs e)
        {
            // MessageBox.Show("Good job");
            NavigationService.Navigate(new Uri("/AddWorkoutPage.xaml", UriKind.Relative));
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string workoutname = vm.Workouts[MainListBox.SelectedIndex].Name;
            NavigationService.Navigate(new Uri("/ModifyWorkoutPage.xaml?selectedItem=" + workoutname, UriKind.Relative));
        }


    }
}